home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / StdAfx.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  5KB  |  167 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // stdafx.cpp : Quelltextdatei, die nur die Standard-Includes einbindet
  20. //    FileZilla server.pch ist die vorcompilierte Header-Datei
  21. //    stdafx.obj enthΣlt die vorcompilierte Typinformation
  22.  
  23. #include "stdafx.h"
  24.  
  25.  
  26.  
  27. HWND hMainWnd = NULL;
  28.  
  29. CCriticalSectionWrapper::CCriticalSectionWrapper()
  30. {
  31.     m_bInitialized = TRUE;
  32.     InitializeCriticalSection(&m_criticalSection);
  33. #ifdef DEBUG
  34.     m_lockCount = 0;
  35. #endif
  36. }
  37.  
  38. CCriticalSectionWrapper::~CCriticalSectionWrapper()
  39. {
  40.     if (m_bInitialized)
  41.         DeleteCriticalSection(&m_criticalSection);
  42.     m_bInitialized = FALSE;
  43. }
  44.  
  45. void CCriticalSectionWrapper::Lock()
  46. {
  47.     if (!m_bInitialized)
  48.         return;
  49.  
  50.     EnterCriticalSection(&m_criticalSection);
  51. #ifdef DEBUG
  52.     m_lockCount++;
  53. #endif
  54. }
  55.  
  56. void CCriticalSectionWrapper::Unlock()
  57. {
  58.     if (!m_bInitialized)
  59.         return;
  60.  
  61. #ifdef DEBUG
  62.     if (m_criticalSection.OwningThread != (HANDLE)GetCurrentThreadId())
  63.     {
  64.         // Suspend thread to avoid further damage
  65.         SuspendThread(GetCurrentThread());
  66.     }
  67.     if (m_lockCount < 1)
  68.     {
  69.         // Suspend thread to avoid further damage
  70.         SuspendThread(GetCurrentThread());
  71.     }
  72.     m_lockCount--;
  73. #endif
  74.     LeaveCriticalSection(&m_criticalSection);
  75. }
  76.  
  77. #ifdef DEADLOCKDEBUG
  78.  
  79. CCriticalSectionWrapper deadlocklock; //pun intended
  80. void EnterCritSectionDebug(CCriticalSectionWrapper §ion, const char *pFile, int line)
  81. {
  82.     CStdString fn;
  83.     fn.Format("c:\\fz%u.txt", (unsigned int)§ion.m_criticalSection);
  84.     deadlocklock.Lock();
  85.     HANDLE hFile = CreateFile(fn, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
  86.     if (hFile != INVALID_HANDLE_VALUE)
  87.     {
  88.         // Avoid overflow, halt server
  89.         int len = GetFileSize(hFile, 0);
  90.         if ((len / 100) > ((len % 100) * 3) + 95)
  91.         {
  92.             SuspendThread(GetCurrentThread());
  93.             while (true)
  94.                 Sleep(1000);
  95.         }
  96.  
  97.         SetFilePointer(hFile, 0, 0, FILE_END);
  98.         CStdString fn2 = pFile;
  99.         int pos = fn2.ReverseFind('\\');
  100.         if (pos != -1)
  101.             fn2 = fn2.Mid(pos + 1);
  102.  
  103.         CStdString str;
  104.         str.Format("locking in %s:%d", fn2.c_str(), line);
  105.         while (str.GetLength() < 98)
  106.             str += " ";
  107.         str += "\r\n";
  108.         DWORD numwritten = 0;
  109.         WriteFile(hFile, str, 100, &numwritten, 0);
  110.         CloseHandle(hFile);
  111.     }
  112.     deadlocklock.Unlock();
  113.     section.Lock();
  114.     deadlocklock.Lock();
  115.     hFile = CreateFile(fn, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
  116.     if (hFile != INVALID_HANDLE_VALUE)
  117.     {
  118.         SetFilePointer(hFile, 0, 0, FILE_END);
  119.         CStdString fn2 = pFile;
  120.         int pos = fn2.ReverseFind('\\');
  121.         if (pos != -1)
  122.             fn2 = fn2.Mid(pos + 1);
  123.  
  124.         CStdString str;
  125.         str.Format("lock obtained in %s:%d", fn2.c_str(), line);
  126.         while (str.GetLength() < 98)
  127.             str += " ";
  128.         str += "\r\n";
  129.         DWORD numwritten = 0;
  130.         WriteFile(hFile, str, 100, &numwritten, 0);
  131.         CloseHandle(hFile);
  132.     }
  133.     deadlocklock.Unlock();
  134. }
  135.  
  136. void LeaveCritSectionDebug(CCriticalSectionWrapper §ion, const char *pFile, int line)
  137. {
  138.     section.Unlock();
  139.  
  140.     deadlocklock.Lock();
  141.     CStdString fn;
  142.     fn.Format("c:\\fz%u.txt", (unsigned int)§ion.m_criticalSection);
  143.     HANDLE hFile = CreateFile(fn, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
  144.     if (hFile != INVALID_HANDLE_VALUE)
  145.     {
  146.         SetFilePointer(hFile, 0, 0, FILE_END);
  147.         CStdString fn2 = pFile;
  148.         int pos = fn2.ReverseFind('\\');
  149.         if (pos != -1)
  150.             fn2 = fn2.Mid(pos + 1);
  151.  
  152.         CStdString str;
  153.         str.Format("unlocked in %s:%d", fn2.c_str(), line);
  154.         while (str.GetLength() < 99)
  155.             str += " ";
  156.         str += "\r\n";
  157.         DWORD numwritten = 0;
  158.         WriteFile(hFile, str, 101, &numwritten, 0);
  159.         int len = GetFileSize(hFile, 0);
  160.         CloseHandle(hFile);
  161.         if ((len / 100) == (len % 100) * 3)
  162.             DeleteFile(fn);
  163.     }
  164.     deadlocklock.Unlock();
  165. }
  166.  
  167. #endif